﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TagBasedResourceManagePlatform
{
    public class Configuration
    {
        #region Nested Object

        private class _SettingNameAttribute: Attribute
        {
            public string Name { get; private set; }
            public string DefaultValue { get; private set; }

            public _SettingNameAttribute(string name, string defaultValue)
            {
                Name = name;
                DefaultValue = defaultValue;
            }
        }

        private class _SettingItems
        {
            public const string ThumbnailDataFolderKey = "thumbnail-data-folder";
            public const string ThunbnailTempFileIdKey = "thumbnail-file-id";

            [_SettingName(ThumbnailDataFolderKey, "@thumbnails")]
            public string ThumbnailDataFolder;

            [_SettingName(ThunbnailTempFileIdKey, "0")]
            public string ThumbnailTempFileId;
            public readonly object ThumbnailTempFileIdChangeLocker = new object();

        }

        #endregion

        #region Fields

        private _SettingItems _settings = new _SettingItems();

        #endregion

        #region Properties

        public string ThumbnailDataFolder {
            get
            {
                return _settings.ThumbnailDataFolder;
            }
            set
            {
                PrivateSetSettingValue(_SettingItems.ThumbnailDataFolderKey, value);
            }
        }

        #endregion

        #region Contructor

        internal Configuration()
        {
            PrivateInitialize();
        }

        #endregion

        #region Private Functions

        private void PrivateSetSettingValue(string name, string value)
        {
            var database = Framework.GetInstance().Database;
            database.SetSettingValue(name, value);
        }

        private void PrivateInitialize()
        {
            var database = Framework.GetInstance().Database;
            var fieldsInfo = (typeof(_SettingItems)).GetFields();
            foreach(var field in fieldsInfo)
            {
                foreach(var attribute in field.GetCustomAttributes(true))
                {
                    var _settingAttribute = attribute as _SettingNameAttribute;
                    if (_settingAttribute != null)
                    {
                        var value = database.GetSettingValue(_settingAttribute.Name, _settingAttribute.DefaultValue);
                        field.SetValue(this._settings, value);
                    }
                }
            }

            
        }


        #endregion

        #region Public Functions

        public long GetNextThumbnailFileId()
        {
            lock (_settings.ThumbnailTempFileIdChangeLocker)
            {
                long current = Convert.ToInt64(_settings.ThumbnailTempFileId);

                PrivateSetSettingValue(_SettingItems.ThunbnailTempFileIdKey, Convert.ToString(current + 1));

                return current;
            }
        }

        public string GetNextThumbnailFilePath()
        {
            if (ThumbnailDataFolder.StartsWith("@"))
            {
                return string.Format("{0}\\{1}\\{2}.thumb", Environment.CurrentDirectory, ThumbnailDataFolder.Substring(1), GetNextThumbnailFileId().ToString("x8"));
            }else
                return string.Format("{0}\\{1}.thumb", ThumbnailDataFolder, GetNextThumbnailFileId().ToString("x8"));
        }        
        #endregion

    }
}
